package com.song.ejf.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.song.ejf.dao.BaseDAO;
import com.song.ejf.page.Page;
import com.song.ejf.pojo.SMenu;
import com.song.ejf.service.MenuService;
@Service("menuService")
public class MenuServiceImpl implements MenuService{
	@Autowired
	BaseDAO baseDAO;
	
	@Override
	public void loadMenuPage(Page page, String menuName,String resName)  throws Exception{
		Map<String, String> paraMap = new HashMap<String, String>();
		String selectSql = "select id,menu_name,resource_name ";
		String mainSql = " select sm.id,sm.menu_name,sr.resource_name from" +
				" s_menu sm,s_resource sr where sm.res_id = sr.r_id ";
		if(StringUtils.isNotBlank(menuName)){
			paraMap.put("menuName", "%" + menuName.trim() + "%");
			mainSql += " and sm.menu_name like :menuName ";
		}
		if(StringUtils.isNotBlank(resName)){
			paraMap.put("resName", "%" + resName.trim() + "%");
			mainSql += " and sr.resource_name like :resName ";
		}
		String order = " order by srg.resource_group_name ";
//		String pageSql =  " where RN > " + page.getStartIndex() + " and RN < " + page.getEndIndex() + " ";
		baseDAO.sqlPageQuery(page, selectSql, mainSql, order, paraMap);
	}

	@Override
	public boolean delMenu(String[] checkItems)  throws Exception{
		if(checkItems.length <= 0){
			return false;
		}
		for (int i = 0; i < checkItems.length; i++) {
			baseDAO.delById(SMenu.class, checkItems[i]);
		}
		return true;
	}

	@Override
	public void saveOrUpdateMenu(SMenu smenu) throws Exception {
		baseDAO.saveOrUpdate(smenu);
	}

	@Override
	public SMenu findMenuById(String id) throws Exception {
		return (SMenu)baseDAO.loadById(SMenu.class, id);
	}


}
